package com.baoyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baoyang.entity.BaoyangPermission;
import com.baoyang.entity.BaoyangUser;
import com.baoyang.entity.PermissionMenu;
import com.baoyang.entity.vo.PermissionMenuReqVO;
import com.baoyang.mapper.BaoyangPermissionMapper;
import com.baoyang.mapper.PermissionMenuMapper;
import com.baoyang.service.BaoyangPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaoyangPermissionServiceImpl implements BaoyangPermissionService {
    @Resource
    private BaoyangPermissionMapper baoyangPermissionMapper;

    @Resource
    private PermissionMenuMapper permissionMenuMapper;

    @Override
    public List<BaoyangPermission> listAll() {
        return baoyangPermissionMapper.selectList(null);
    }

    @Transactional
    @Override
    public boolean save(PermissionMenuReqVO reqVO) {
        if (reqVO.getMenuIds().size() == 0){
            throw new RuntimeException("菜单不能为空");
        }
        List<BaoyangPermission> permissionList = baoyangPermissionMapper.selectList(new QueryWrapper<BaoyangPermission>().eq("name", reqVO.getName()));
        if (CollectionUtils.isNotEmpty(permissionList)){
            throw new RuntimeException("角色名重复");
        }
        BaoyangPermission permission = new BaoyangPermission();
        BeanUtils.copyProperties(reqVO, permission);
        baoyangPermissionMapper.insert(permission);
        Long permissionId = permission.getId();
        /* 关联权限和菜单 */
        for (Long menuId : reqVO.getMenuIds()) {
            PermissionMenu permissionMenu = new PermissionMenu();
            permissionMenu.setPermissionId(permissionId);
            permissionMenu.setMenuId(menuId);
            permissionMenuMapper.insert(permissionMenu);
        }
        return true;
    }

    @Override
    public boolean removeById(String id) {
        if (baoyangPermissionMapper.deleteById(id) > 0) {
            permissionMenuMapper.delete(new QueryWrapper<PermissionMenu>().eq("permission_id", id));
            return true;
        }
        return false;
    }

    @Override
    public List<String> getMenusByRoleId(String roleId) {
        List<PermissionMenu> permissionMenus = permissionMenuMapper.selectList(new QueryWrapper<PermissionMenu>().eq("permission_id", roleId));
        if (CollectionUtils.isNotEmpty(permissionMenus)){
            return permissionMenus.stream().map(PermissionMenu::getMenuId).map(String::valueOf).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public boolean editRole(PermissionMenuReqVO reqVO) {
        BaoyangPermission permission = baoyangPermissionMapper.selectById(reqVO.getId());
        permission.setName(reqVO.getName());
        permission.setDataScope(reqVO.getDataScope());
        permission.setRemark(reqVO.getRemark());
        baoyangPermissionMapper.updateById(permission);
        /* 菜单 */
        List<PermissionMenu> permissionMenus = permissionMenuMapper.selectList(new QueryWrapper<PermissionMenu>().lambda().eq(PermissionMenu::getPermissionId, permission.getId()));
        List<Long> deleteIds = permissionMenus.stream().map(PermissionMenu::getId).collect(Collectors.toList());
        permissionMenuMapper.deleteBatchIds(deleteIds);
        /* 关联权限和菜单 */
        for (Long menuId : reqVO.getMenuIds()) {
            PermissionMenu permissionMenu = new PermissionMenu();
            permissionMenu.setPermissionId(reqVO.getId());
            permissionMenu.setMenuId(menuId);
            permissionMenuMapper.insert(permissionMenu);
        }
        return true;
    }

    @Override
    public IPage<BaoyangPermission> pageUserList(Page<BaoyangPermission> page, String name) {
        QueryWrapper<BaoyangPermission> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("create_time");
        Page<BaoyangPermission> baoyangPermissionPage = baoyangPermissionMapper.selectPage(page, queryWrapper);
        List<BaoyangPermission> records = baoyangPermissionPage.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            for (BaoyangPermission permission:records) {
                if (permission.getDataScope().equals("1")){
                    permission.setDataScopeName("全部数据");
                }else if (permission.getDataScope().equals("2")){
                    permission.setDataScopeName("个人数据");
                }
            }
            baoyangPermissionPage.setRecords(records);
        }
        return baoyangPermissionPage;
    }
}